library(lfe)
library(DIDmultiplegt)
library(bacondecomp)
library(did)
library(xtable)
library(lmtest)
library(sandwich)
library(stargazer)
library(tidyverse)


rm(list = ls())
load("main_data.RData")

#TABLE 2####
#count
model_count <- felm(afd_second ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_count <- coeftest(model_count, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013","2014", "2016", "2017", "2019")])

#binary
model_binary <- felm(afd_second ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_binary <- coeftest(model_binary, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#points
model_points <- felm(afd_second ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_points <- coeftest(model_points, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#for all elections 
#count
model_count_all <- felm(afd_second ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7 , ])
coef_count_all <- coeftest(model_count_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#binary
model_binary_all <- felm(afd_second ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_binary_all <- coeftest(model_binary_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#points
model_points_all <- felm(afd_second ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_points_all <- coeftest(model_points_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

stargazer(model_count, model_binary, model_points, model_count_all, model_binary_all, model_points_all,
          se = list(coef_count[,2], coef_binary[,2], coef_points[,2], coef_count_all[,2], coef_binary_all[,2], coef_points_all[,2]),
          p = list(coef_count[,4], coef_binary[,4], coef_points[,4], coef_count_all[,4], coef_binary_all[,4], coef_points_all[,4]),
          out = "panel_results.tex",
          star.cutoffs = c(.05, .01, .001))
rm(list = setdiff(ls(), "elections"))

#TABLE S1####
#count
model_count <- felm(afd_share ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_count <- coeftest(model_count, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013","2014", "2016", "2017", "2019")])

#binary
model_binary <- felm(afd_share ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_binary <- coeftest(model_binary, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#points
model_points <- felm(afd_share ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_points <- coeftest(model_points, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#for all elections 
#count
model_count_all <- felm(afd_share ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7 , ])
coef_count_all <- coeftest(model_count_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#binary
model_binary_all <- felm(afd_share ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_binary_all <- coeftest(model_binary_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#points
model_points_all <- felm(afd_share ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_points_all <- coeftest(model_points_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

stargazer(model_count, model_binary, model_points, model_count_all, model_binary_all, model_points_all,
          se = list(coef_count[,2], coef_binary[,2], coef_points[,2], coef_count_all[,2], coef_binary_all[,2], coef_points_all[,2]),
          p = list(coef_count[,4], coef_binary[,4], coef_points[,4], coef_count_all[,4], coef_binary_all[,4], coef_points_all[,4]),
          out = "panel_results_first.tex",
          star.cutoffs = c(.05, .01, .001))
rm(list = setdiff(ls(), "elections"))

#TABLE S2####
##count
model_count <- felm(right_total ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_count <- coeftest(model_count, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013","2014", "2016", "2017", "2019")])

#binary
model_binary <- felm(right_total ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_binary <- coeftest(model_binary, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#points
model_points <- felm(right_total ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_points <- coeftest(model_points, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#for all elections 
#count
model_count_all <- felm(right_total ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7 , ])
coef_count_all <- coeftest(model_count_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#binary
model_binary_all <- felm(right_total ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_binary_all <- coeftest(model_binary_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#points
model_points_all <- felm(right_total ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_points_all <- coeftest(model_points_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

stargazer(model_count, model_binary, model_points, model_count_all, model_binary_all, model_points_all,
          se = list(coef_count[,2], coef_binary[,2], coef_points[,2], coef_count_all[,2], coef_binary_all[,2], coef_points_all[,2]),
          p = list(coef_count[,4], coef_binary[,4], coef_points[,4], coef_count_all[,4], coef_binary_all[,4], coef_points_all[,4]),
          out = "panel_results_right.tex",
          star.cutoffs = c(.05, .01, .001))
rm(list = setdiff(ls(), "elections"))

#TABLE S11####
#count
model_count <- felm(afd_second_count ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_count <- coeftest(model_count, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013","2014", "2016", "2017", "2019")])

#binary
model_binary <- felm(afd_second_count ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_binary <- coeftest(model_binary, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#points
model_points <- felm(afd_second_count ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_points <- coeftest(model_points, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#for all elections 
#count
model_count_all <- felm(afd_second_count ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7 , ])
coef_count_all <- coeftest(model_count_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#binary
model_binary_all <- felm(afd_second_count ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_binary_all <- coeftest(model_binary_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#points
model_points_all <- felm(afd_second_count ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_points_all <- coeftest(model_points_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

stargazer(model_count, model_binary, model_points, model_count_all, model_binary_all, model_points_all,
          se = list(coef_count[,2], coef_binary[,2], coef_points[,2], coef_count_all[,2], coef_binary_all[,2], coef_points_all[,2]),
          p = list(coef_count[,4], coef_binary[,4], coef_points[,4], coef_count_all[,4], coef_binary_all[,4], coef_points_all[,4]),
          out = "panel_afd_count.tex",
          star.cutoffs = c(.05, .01, .001))
rm(list = setdiff(ls(), "elections"))


#TABLE S9####
#count
model_count <- felm(total_voter_second ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_count <- coeftest(model_count, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013","2014", "2016", "2017", "2019")])

#binary
model_binary <- felm(total_voter_second ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_binary <- coeftest(model_binary, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#points
model_points <- felm(total_voter_second ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_points <- coeftest(model_points, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#for all elections 
#count
model_count_all <- felm(total_voter_second ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7 , ])
coef_count_all <- coeftest(model_count_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#binary
model_binary_all <- felm(total_voter_second ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_binary_all <- coeftest(model_binary_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#points
model_points_all <- felm(total_voter_second ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_points_all <- coeftest(model_points_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

stargazer(model_count, model_binary, model_points, model_count_all, model_binary_all, model_points_all,
          se = list(coef_count[,2], coef_binary[,2], coef_points[,2], coef_count_all[,2], coef_binary_all[,2], coef_points_all[,2]),
          p = list(coef_count[,4], coef_binary[,4], coef_points[,4], coef_count_all[,4], coef_binary_all[,4], coef_points_all[,4]),
          out = "panel_total_voters.tex",
          star.cutoffs = c(.05, .01, .001))
rm(list = setdiff(ls(), "elections"))


#TABLE A10###
#count
model_count <- felm(turnout ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_count <- coeftest(model_count, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013","2014", "2016", "2017", "2019")])

#binary
model_binary <- felm(turnout ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_binary <- coeftest(model_binary, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#points
model_points <- felm(turnout ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_points <- coeftest(model_points, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#for all elections 
#count
model_count_all <- felm(turnout ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7 , ])
coef_count_all <- coeftest(model_count_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#binary
model_binary_all <- felm(turnout ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_binary_all <- coeftest(model_binary_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#points
model_points_all <- felm(turnout ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_points_all <- coeftest(model_points_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

stargazer(model_count, model_binary, model_points, model_count_all, model_binary_all, model_points_all,
          se = list(coef_count[,2], coef_binary[,2], coef_points[,2], coef_count_all[,2], coef_binary_all[,2], coef_points_all[,2]),
          p = list(coef_count[,4], coef_binary[,4], coef_points[,4], coef_count_all[,4], coef_binary_all[,4], coef_points_all[,4]),
          out = "panel_turnout.tex",
          star.cutoffs = c(.05, .01, .001))
rm(list = setdiff(ls(), "elections"))


#TABLE S12####
#count
model_count <- felm(cdu_second ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_count <- coeftest(model_count, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013","2014", "2016", "2017", "2019")])

#binary
model_binary <- felm(cdu_second ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_binary <- coeftest(model_binary, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#points
model_points <- felm(cdu_second ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_points <- coeftest(model_points, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#for all elections 
#count
model_count_all <- felm(cdu_second ~ stone_count_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7 , ])
coef_count_all <- coeftest(model_count_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#binary
model_binary_all <- felm(cdu_second ~ stone_binary + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_binary_all <- coeftest(model_binary_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#points
model_points_all <- felm(cdu_second ~ unique_points_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_points_all <- coeftest(model_points_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

stargazer(model_count, model_binary, model_points, model_count_all, model_binary_all, model_points_all,
          se = list(coef_count[,2], coef_binary[,2], coef_points[,2], coef_count_all[,2], coef_binary_all[,2], coef_points_all[,2]),
          p = list(coef_count[,4], coef_binary[,4], coef_points[,4], coef_count_all[,4], coef_binary_all[,4], coef_points_all[,4]),
          out = "panel_cdu.tex",
          star.cutoffs = c(.05, .01, .001))
rm(list = setdiff(ls(), "elections"))


#TABLE S3#####
#first five elections
subelect <- elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ]
subelect$count_cat <- ifelse(subelect$stone_count == 0, "Control",
                             ifelse(subelect$stone_count <= quantile(subelect$stone_count[subelect$stone_count > 0], .33), "Low Count",
                                    ifelse(subelect$stone_count > quantile(subelect$stone_count[subelect$stone_count > 0], .33) & subelect$stone_count <= quantile(subelect$stone_count[subelect$stone_count > 0], .66), "Mid Count", "High Count")))
subelect$point_cat <- ifelse(subelect$unique_points == 0, "Control",
                             ifelse(subelect$unique_points <= quantile(subelect$unique_points[subelect$unique_points > 0], .33), "Low Count",
                                    ifelse(subelect$unique_points > quantile(subelect$unique_points[subelect$unique_points > 0], .33) & subelect$unique_points <= quantile(subelect$unique_points[subelect$unique_points > 0], .66), "Mid Count", "High Count")))

model_count <- felm(afd_second ~ as.factor(count_cat) + as.factor(election) | STI | 0 |  STI, data = subelect)
coef_count <- coeftest(model_count, vcov = vcovCL, cluster = subelect$STI)

model_points <- felm(afd_second ~ as.factor(point_cat) + as.factor(election) | STI | 0 |  STI, data = subelect)
coef_points <- coeftest(model_points, vcov = vcovCL, cluster = subelect$STI)

#all elections
subelect <- elections[elections$n == 7,  ]
subelect$count_cat <- ifelse(subelect$stone_count == 0, "Control",
                             ifelse(subelect$stone_count <= quantile(subelect$stone_count[subelect$stone_count > 0], .33), "Low Count",
                                    ifelse(subelect$stone_count > quantile(subelect$stone_count[subelect$stone_count > 0], .33) & subelect$stone_count <= quantile(subelect$stone_count[subelect$stone_count > 0], .66), "Mid Count", "High Count")))

subelect$point_cat <- ifelse(subelect$unique_points == 0, "Control",
                             ifelse(subelect$unique_points <= quantile(subelect$unique_points[subelect$unique_points > 0], .33), "Low Count",
                                    ifelse(subelect$unique_points > quantile(subelect$unique_points[subelect$unique_points > 0], .33) & subelect$unique_points <= quantile(subelect$unique_points[subelect$unique_points > 0], .66), "Mid Count", "High Count")))

model_count_all <- felm(afd_second ~ as.factor(count_cat) + as.factor(election) | STI | 0 |  STI, data = subelect)
coef_count_all <- coeftest(model_count_all, vcov = vcovCL, cluster = subelect$STI)

model_points_all <- felm(afd_second ~ as.factor(point_cat) + as.factor(election) | STI | 0 |  STI, data = subelect)
coef_points_all <- coeftest(model_points_all, vcov = vcovCL, cluster = subelect$STI)

#printing the results
stargazer(model_count, model_points, model_count_all, model_points_all,
          se = list(coef_count[,2], coef_points[,2], coef_count_all[,2], coef_points_all[,2]),
          p = list(coef_count[,4], coef_points[,4], coef_count_all[,4], coef_points_all[,4]),
          out = "panel_categorical.tex",
          star.cutoffs = c(.05, .01, .001))
rm(list = setdiff(ls(), "elections"))

#TABLE S4####
#count
model_count <- lm(afd_second ~ stone_count_log, data = elections)
coef_count <- coeftest(model_count, vcov = vcovCL, cluster = elections$election_bwb)

#binary
model_binary <- lm(afd_second ~ stone_binary, data = elections)
coef_binary <- coeftest(model_binary, vcov = vcovCL, cluster = elections$election_bwb)

#unique points
model_points <- lm(afd_second ~ unique_points_log, data = elections)
coef_points <- coeftest(model_points, vcov = vcovCL, cluster = elections$election_bwb)

#count
model_count_fe <- felm(afd_second ~ stone_count_log | election_bwb | 0 |  election_bwb, data = elections)
coef_count_fe <- coeftest(model_count_fe, vcov = vcovCL, cluster = elections$election_bwb)

#binary
model_binary_fe <- felm(afd_second ~ stone_binary | election_bwb | 0 |  election_bwb, data = elections)
coef_binary_fe <- coeftest(model_binary_fe, vcov = vcovCL, cluster = elections$election_bwb)

#unique points
model_points_fe <- felm(afd_second ~ unique_points_log | election_bwb | 0 |  election_bwb, data = elections)
coef_points_fe <- coeftest(model_points_fe, vcov = vcovCL, cluster = elections$election_bwb)


#printing the results
stargazer(model_count, model_binary, model_points, model_count_fe, model_binary_fe, model_points_fe,
          se = list(coef_count[,2], coef_binary[,2], coef_points[,2], coef_count_fe[,2], coef_binary_fe[,2], coef_points_fe[,2]),
          p = list(coef_count[,4], coef_binary[,4], coef_points[,4], coef_count_fe[,4], coef_binary_fe[,4], coef_points_fe[,4]),
          out = "stones_correlation.tex",
          star.cutoffs = c(.05, .01, .001))
rm(list = setdiff(ls(), "elections"))

#TABLE S5####
#count
model_count <- felm(afd_second ~ stone_count_log + neighbor_stone_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_count <- coeftest(model_count, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013","2014", "2016", "2017", "2019")])

#binary
model_binary <- felm(afd_second ~ stone_binary + neighbor_stone_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_binary <- coeftest(model_binary, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#points
model_points <- felm(afd_second ~ unique_points_log + neighbor_stone_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_points <- coeftest(model_points, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#for all elections 
#count
model_count_all <- felm(afd_second ~ stone_count_log + neighbor_stone_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7 , ])
coef_count_all <- coeftest(model_count_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#binary
model_binary_all <- felm(afd_second ~ stone_binary + neighbor_stone_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_binary_all <- coeftest(model_binary_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#points
model_points_all <- felm(afd_second ~ unique_points_log + neighbor_stone_log + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_points_all <- coeftest(model_points_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

stargazer(model_count, model_binary, model_points, model_count_all, model_binary_all, model_points_all,
          se = list(coef_count[,2], coef_binary[,2], coef_points[,2], coef_count_all[,2], coef_binary_all[,2], coef_points_all[,2]),
          p = list(coef_count[,4], coef_binary[,4], coef_points[,4], coef_count_all[,4], coef_binary_all[,4], coef_points_all[,4]),
          out = "neighbor_panel_results.tex",
          star.cutoffs = c(.05, .01, .001))
rm(list = setdiff(ls(), "elections"))

#TABLE S6####
#count
model_count <- felm(afd_second ~ stone_count_log + percentage_neighbor + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_count <- coeftest(model_count, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013","2014", "2016", "2017", "2019")])

#binary
model_binary <- felm(afd_second ~ stone_binary + percentage_neighbor + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_binary <- coeftest(model_binary, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#points
model_points <- felm(afd_second ~ unique_points_log + percentage_neighbor + as.factor(election) | STI | 0 |  STI, data = elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ])
coef_points <- coeftest(model_points, vcov = vcovCL, cluster = elections$STI[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019")])

#for all elections 
#count
model_count_all <- felm(afd_second ~ stone_count_log + percentage_neighbor + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7 , ])
coef_count_all <- coeftest(model_count_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#binary
model_binary_all <- felm(afd_second ~ stone_binary + percentage_neighbor + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_binary_all <- coeftest(model_binary_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

#points
model_points_all <- felm(afd_second ~ unique_points_log + percentage_neighbor + as.factor(election) | STI | 0 |  STI, data = elections[elections$n == 7,])
coef_points_all <- coeftest(model_points_all, vcov = vcovCL, cluster = elections$STI[elections$n == 7])

stargazer(model_count, model_binary, model_points, model_count_all, model_binary_all, model_points_all,
          se = list(coef_count[,2], coef_binary[,2], coef_points[,2], coef_count_all[,2], coef_binary_all[,2], coef_points_all[,2]),
          p = list(coef_count[,4], coef_binary[,4], coef_points[,4], coef_count_all[,4], coef_binary_all[,4], coef_points_all[,4]),
          out =  "neighbor_percentage_panel_results.tex",
          star.cutoffs = c(.05, .01, .001))
rm(list = setdiff(ls(), "elections"))



#TABLE S7####
#this analysis takes quite some time
set.seed(123)
count_five <- did_multiplegt(elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ],
                             Y = "afd_second", G = "STI", T = "election", D = "stone_count_log", brep = 100, cluster = "STI")

set.seed(123)
binary_five <- did_multiplegt(elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ],
                              Y = "afd_second", G = "STI", T = "election", D = "stone_binary", brep = 100, cluster = "STI")

set.seed(123)
points_five <- did_multiplegt(elections[elections$n %in% c(5, 7) & elections$election %in% c("2013", "2014", "2016", "2017", "2019"),  ],
                              Y = "afd_second", G = "STI", T = "election", D = "unique_points_log", brep = 100, cluster = "STI")

set.seed(123)
count_seven <- did_multiplegt(elections[elections$n == 7 , ],
                              Y = "afd_second", G = "STI", T = "election", D = "stone_count_log", brep = 100, cluster = "STI")

set.seed(123)
binary_seven <- did_multiplegt(elections[elections$n == 7 , ],
                               Y = "afd_second", G = "STI", T = "election", D = "stone_binary", brep = 100, cluster = "STI")

set.seed(123)
points_seven <- did_multiplegt(elections[elections$n == 7 , ],
                               Y = "afd_second", G = "STI", T = "election", D = "unique_points_log", brep = 100, cluster = "STI")

#a table to write
coefs <- round(c(count_five$effect, binary_five$effect, points_five$effect,
                 count_seven$effect, binary_seven$effect, points_seven$effect), 3)
ses <- str_c("(", round(c(count_five$se_effect, binary_five$se_effect, points_five$se_effect,
                          count_seven$se_effect, binary_seven$se_effect, points_seven$se_effect), 3),  ")")

to_write <- data.frame(rbind(coefs, ses))
xtable(print(to_write), file = "twfe_check.tex")
rm(list = setdiff(ls(), "elections"))


#TABLE S8####
#for four elections
subelect <- elections[elections$n %in% c(5, 7) & elections$election %in% c("2013","2014", "2016", "2017", "2019"), ]
subelect$election_num <- 1
subelect$election_num[subelect$election == "2014"] <- 2
subelect$election_num[subelect$election == "2016"] <- 3
subelect$election_num[subelect$election == "2017"] <- 4
subelect$election_num[subelect$election == "2019"] <- 5

to_merge <- select(subelect, election_num, STI, stone_binary)
to_merge$election_num <- to_merge$election_num+1
to_merge <- rename(to_merge, stone_lag = stone_binary)
subelect <- merge(subelect, to_merge, by = c("STI", "election_num"), all.x = T)
subelect$difference <- subelect$stone_binary - subelect$stone_lag
subelect$difference[is.na(subelect$difference)] <- 0
subelect <- subelect %>%
  group_by(STI) %>%
  mutate(to_drop = min(difference))
subelect <- subelect[subelect$to_drop == 0,]


df_bacon <- bacon(afd_second ~ stone_binary,
                  data = subelect,
                  id_var = "STI",
                  time_var = "election_num")
df_bacon_five <- df_bacon %>%
  group_by(type) %>%
  summarize(avg_est = weighted.mean(estimate, weight), weight = sum(weight))
df_bacon_five <- rename(df_bacon_five, avg_est_five = avg_est, weight_five = weight)
rm(list = setdiff(ls(), c("elections", "df_bacon_five")))

#for all elections
subelect <- elections[elections$n == 7,]
subelect$election_num <- 1
subelect$election_num[subelect$election == "2014"] <- 2
subelect$election_num[subelect$election == "2016"] <- 3
subelect$election_num[subelect$election == "2017"] <- 4
subelect$election_num[subelect$election == "2019"] <- 5
subelect$election_num[subelect$election == "2021"] <- 6
subelect$election_num[subelect$election == "2021-Local"] <- 6

to_merge <- select(subelect, election_num, STI, stone_binary)
to_merge$election_num <- to_merge$election_num+1
to_merge <- rename(to_merge, stone_lag = stone_binary)
subelect <- merge(subelect, to_merge, by = c("STI", "election_num"), all.x = T)
subelect$difference <- subelect$stone_binary - subelect$stone_lag
subelect$difference[is.na(subelect$difference)] <- 0
subelect <- subelect %>%
  group_by(STI) %>%
  mutate(to_drop = min(difference))
subelect <- subelect[subelect$to_drop == 0,]

df_bacon <- bacon(afd_second ~ stone_binary,
                  data = subelect,
                  id_var = "STI",
                  time_var = "election_num")
df_bacon <- df_bacon %>%
  group_by(type) %>%
  summarize(avg_est = weighted.mean(estimate, weight), weight = sum(weight))
df_bacon_five <- merge(df_bacon_five, df_bacon, by = "type")
print(xtable(df_bacon_five, digits = 3), include.rownames = F, file = "goodman_bacon.tex", digits = 5)
rm(list = setdiff(ls(), "elections"))

#FIGURE S7####
subelect <- elections[elections$n == 7,]
subelect$election_num <- 1
subelect$election_num[subelect$election == "2014"] <- 2
subelect$election_num[subelect$election == "2016"] <- 3
subelect$election_num[subelect$election == "2017"] <- 4
subelect$election_num[subelect$election == "2019"] <- 5
subelect$election_num[subelect$election == "2021"] <- 6
subelect$election_num[subelect$election == "2021-Local"] <- 6

to_merge <- select(subelect, election_num, STI, stone_binary)
to_merge$election_num <- to_merge$election_num+1
to_merge <- rename(to_merge, stone_lag = stone_binary)
subelect <- merge(subelect, to_merge, by = c("STI", "election_num"), all.x = T)
subelect$difference <- subelect$stone_binary - subelect$stone_lag
subelect$difference[is.na(subelect$difference)] <- 0
subelect <- subelect %>%
  group_by(STI) %>%
  mutate(to_drop = min(difference))
subelect <- subelect[subelect$to_drop == 0,]

diff <- subelect[subelect$difference == 1, ]
diff <- diff %>%
  group_by(STI) %>%
  summarize(election_num = min(election_num))
diff <- select(diff, STI, election_num)
diff <- rename(diff, first_treat  = election_num)
subelect <- merge(subelect, diff, by = "STI", all.x = T)
subelect$first_treat[is.na(subelect$first_treat)] <- 0
subelect$STI <- as.numeric(subelect$STI)
subelect <- subelect %>%
  group_by(STI) %>%
  mutate(av_stone = mean(stone_binary))
subelect$first_treat[subelect$av_stone == 1] <- 1

out <- att_gt(yname = "afd_second",
              gname = "first_treat",
              idname = "STI",
              tname = "election_num",
              xformla = ~ 1,
              data = subelect,
              est_method = "reg",
              clustervars = "STI",
              bstrap = T,
              allow_unbalanced_panel = T
)


es <- aggte(out, type = "dynamic", clustervars = "STI", bstrap = T)
ggdid(es)

